package com.carbeauty.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import net.semanticmetadata.lire.DocumentBuilder;
import net.semanticmetadata.lire.ImageSearchHits;
import net.semanticmetadata.lire.ImageSearcher;
import net.semanticmetadata.lire.ImageSearcherFactory;
import net.semanticmetadata.lire.imageanalysis.bovw.SurfFeatureHistogramBuilder;
import net.semanticmetadata.lire.impl.VisualWordsImageSearcher;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.FSDirectory;

import com.carbeauty.util.FileUtil;

public class ImageSearch {
	private ImageSearchHits hits;
	private ImageSearcher getSearcher(int searchType,int numResults) {
        if(numResults==0)
        {
        	numResults=50;
        }
        ImageSearcher searcher = ImageSearcherFactory.createScalableColorImageSearcher(numResults);
        if (searchType == 1) {
            searcher = ImageSearcherFactory.createScalableColorImageSearcher(numResults);
        } else if (searchType == 2) {
            searcher = ImageSearcherFactory.createEdgeHistogramImageSearcher(numResults);
        } else if (searchType == 3) {
            searcher = ImageSearcherFactory.createAutoColorCorrelogramImageSearcher(numResults);
        } else if (searchType == 4) { // CEDD
            searcher = ImageSearcherFactory.createCEDDImageSearcher(numResults);
        } else if (searchType == 5) { // FCTH
            searcher = ImageSearcherFactory.createFCTHImageSearcher(numResults);
        } else if (searchType == 6) { // JCD
            searcher = ImageSearcherFactory.createJCDImageSearcher(numResults);
        } else if (searchType == 7) { // SimpleColorHistogram
            searcher = ImageSearcherFactory.createColorHistogramImageSearcher(numResults);
        } else if (searchType == 8) {
            searcher = ImageSearcherFactory.createTamuraImageSearcher(numResults);
        } else if (searchType == 9) {
            searcher = ImageSearcherFactory.createGaborImageSearcher(numResults);
        } else if (searchType == 10) {
            searcher = ImageSearcherFactory.createJpegCoefficientHistogramImageSearcher(numResults);
        } else if (searchType > 10) {
            searcher = new VisualWordsImageSearcher(numResults, DocumentBuilder.FIELD_NAME_SURF_LOCAL_FEATURE_HISTOGRAM_VISUAL_WORDS);
        }
        return searcher;
    }
	
    public ImageSearch searchImage(String fileName,int searchType,int numResults,boolean isTemp) throws FileNotFoundException, IOException {
        try {
            IndexReader reader = IndexReader.open(FSDirectory.open(new File(Constants.INDEX_NAME)), true);
            ImageSearcher searcher = getSearcher(4,numResults);
              hits = searcher.search(ImageIO.read(new FileInputStream((isTemp?Constants.FILE_UPLOAD_TEMP_DIR:Constants.FILE_UPLOAD_DIR)+File.separator+fileName)), reader);
            reader.close();
            if(isTemp)
            {
            	//FileUtil.deleteFile(Constants.FILE_UPLOAD_TEMP_DIR+File.separator+fileName);
            }
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
        }
        return this;
    }
  
    public ArrayList<String> getSearchResult() {
    	ArrayList<String> images = new ArrayList<String>();
    	if(hits==null)
    	{
    		return images;
    	}
    	
        for (int i = 0; i < hits.length(); i++) {
            try {
                String fileIdentifier = hits.doc(i).getFieldable(DocumentBuilder.FIELD_NAME_IDENTIFIER).stringValue();
                images.add(fileIdentifier.substring(fileIdentifier.lastIndexOf(File.separator)+1, fileIdentifier.length()));
            } catch (Exception ex) {
                Logger.getLogger("global").log(Level.SEVERE, null, ex);
            }
            
        }
        
        return images;
    }
    

    
   public static void main(String[] args)
   {
	   
	   String asd="/asdfa/asdfas/asdf.jpg";
	   System.out.println(asd.substring(asd.lastIndexOf("/"), asd.length()));
//	   try {
//		 
//		   for(int i = 1 ; i < 10;i++)
//		   {
//			   System.out.println("分类============="+i+"===========");
//		   ImageSearch is = new ImageSearch().searchImage("2.jpg",i, 100);
//		   is.getSearchResult();
//		   }
//	} catch (FileNotFoundException e) {
//		e.printStackTrace();
//	} catch (IOException e) {
//		e.printStackTrace();
//	}
   }
}
